package com.springbootee.uitl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @name: RedisUtil 缓存工具类
 * @author: 你的辰宝
 * @time: 2021/11/4 1:22
 * @deprecated :  注释
 */
@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    public RedisUtil(RedisTemplate<String,Object> redisTemplate){
        this.redisTemplate=redisTemplate;
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param time:
      *@return: * @return: boolean
      *@author: 你的辰宝
      *@time: 2021/11/4 1:24
     * 指定缓存失效的时间
     */
    public boolean expire(String key,long time){
    try{
        if (time > 0){
            redisTemplate.expire(key,time, TimeUnit.SECONDS);
        }
        return  true;
    }catch (Exception e){
        e.printStackTrace();
        return  false;
    }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
      *@return: * @return: long
      *@author: 你的辰宝
      *@time: 2021/11/4 1:29
     * 根据Key获取过期时间
     */
     public  long getExpire(String key){
        return  redisTemplate.getExpire(key, TimeUnit.SECONDS);
     }

     /**
       *@deprecated: RedisUtil
       *@parms: * @Param key:
       *@return: * @return: boolean
       *@author: 你的辰宝
       *@time: 2021/11/4 1:30
        *判断Key是否存在
      */
     public boolean haskey(String key){
         try{
             return redisTemplate.hasKey(key);
         }catch (Exception e){
             e.printStackTrace();
             return false;
         }
     }

     /**
       *@deprecated: RedisUtil
       *@parms: * @Param key: 可以传一个值，也可以传多个
       *@return: * @return: void
       *@author: 你的辰宝
       *@time: 2021/11/4 1:36
        删除缓存
      */
     public void del(String... key){
         if (key != null && key.length >0 ){
             if (key.length ==1){
                 redisTemplate.delete(key[0]);
             }else{
                 redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
          }
         }
     }

     /**
       *@deprecated: RedisUtil
       *@parms: * @Param pattern:
       *@return: * @return: java.util.Set
       *@author: 你的辰宝
       *@time: 2021/11/4 1:39
      * 模糊查询获取key值
      */
     public Set keys(String pattern){
         return  redisTemplate.keys(pattern);
     }

     /**
       *@deprecated: RedisUtil
       *@parms: * @Param channel:
      * @Param message: 消息内容
       *@return: * @return: void
       *@author: 你的辰宝
       *@time: 2021/11/4 1:40
        使用redis消息队列
      */
     public void  convertAndSend(String channel,Object message){
         redisTemplate.convertAndSend(channel,message);
     }

     /**
       *@deprecated: RedisUtil
       *@parms: * @Param key:
       *@return: * @return: java.lang.Object
       *@author: 你的辰宝
       *@time: 2021/11/4 1:41
        普通缓存获取
      */
     public  Object  get(String key){
         return  key == null ? null : redisTemplate.opsForValue().get(key);
     }

     /**
       *@deprecated: RedisUtil
       *@parms: * @Param key:
      * @Param value:
       *@return: * @return: boolean
       *@author: 你的辰宝
       *@time: 2021/11/4 1:43
      普通缓存放入 通过true or false来判断
        */
     public boolean set(String key,Object value){
         try{
             redisTemplate.opsForValue().set(key,value);
             return true;
         }catch (Exception e){
             e.printStackTrace();
             return  false;
         }
     }

     /**
       *@deprecated: RedisUtil
       *@parms: * @Param key:
      * @Param value:
      * @Param time:
       *@return: * @return: boolean
       *@author: 你的辰宝
       *@time: 2021/11/4 1:46
        普通缓存放入并且设置时间
      */
     public  boolean  set(String key,Object value,long time){
         try{
             if (time >0){
                 redisTemplate.opsForValue().set(key,value,time,TimeUnit.SECONDS);
             }else {
                 set(key,value);
             }
             return  true;
         }catch (Exception e){
             e.printStackTrace();
             return  false;
         }
     }

     /**
       *@deprecated: RedisUtil
       *@parms: * @Param key:
      * @Param delta:
       *@return: * @return: long
       *@author: 你的辰宝
       *@time: 2021/11/4 1:49
        递增
      */
     public long incr(String key,long delta){

         if (delta<0){
             throw  new RuntimeException("递增因子必须大于");
         }
         return  redisTemplate.opsForValue().increment(key,delta);

     }

     /**
       *@deprecated: RedisUtil
       *@parms: * @Param key:
      * @Param delta:
       *@return: * @return: long
       *@author: 你的辰宝
       *@time: 2021/11/4 1:51
      递减
      */
     public  long decr(String key,long delta) {
         if (delta  < 0) {
             throw new RuntimeException("递增因子必须小于0");
         }
         return redisTemplate.opsForValue().increment(key,-delta);
     }

     /**
       *@deprecated: RedisUtil
       *@parms: * @Param key:
      * @Param item:
      * @Param value:
       *@return: * @return: boolean
       *@author: 你的辰宝
       *@time: 2021/11/4 1:57
      向一张Hash表中放入数据，如果不存在将创建
      */
     public  boolean hset(String key,String item,Object value){
         try {
             redisTemplate.opsForHash().put(key,item,value);
             return  true;
         }catch (Exception e){
             e.printStackTrace();
             return  false;
         }
     }

    /**
     *@deprecated: RedisUtil
     *@parms: * @Param key:
     * @Param item:
     * @Param value:
     *@return: * @return: boolean
     *@author: 你的辰宝
     *@time: 2021/11/4 1:57
    向一张Hash表中放入数据，如果不存在将创建
     */
    public  boolean hset(String key,String item,Object value,long time  ){
        try {
            redisTemplate.opsForHash().put(key,item,value);
            if (time > 0){
                expire(key,time);
            }
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param item:
      *@return: * @return: java.lang.Object
      *@author: 你的辰宝
      *@time: 2021/11/4 2:03
     HashGet拿到Hash表
     */
    public  Object hget(String key,String item){
        return  redisTemplate.opsForHash().get(key,item);
    }


    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param item:
      *@return: * @return: void
      *@author: 你的辰宝
      *@time: 2021/11/4 2:05
     删除Hash表的值
     */
    public  void  hdel(String key,Object... item){
         redisTemplate.opsForHash().delete(key, item);
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
      *@return: * @return: java.util.Map<java.lang.Object,java.lang.Object>
      *@author: 你的辰宝
      *@time: 2021/11/4 2:06
     获取HashKey对应的键值
     */
    public Map<Object,Object> hmget(String key){
        return  redisTemplate.opsForHash().entries(key);
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param map:
      *@return: * @return: boolean
      *@author: 你的辰宝
      *@time: 2021/11/4 2:08
       HashSet
        */
    public  boolean hmset(String key,Map<Object,Object> map){
        try {
            redisTemplate.opsForHash().putAll(key,map);
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }
    /**
     *@deprecated: RedisUtil
     *@parms: * @Param key:
     * @Param map:
     *@return: * @return: boolean
     *@author: 你的辰宝
     *@time: 2021/11/4 2:08.
    HashSet
     */
    public  boolean hmset(String key,Map<Object,Object> map,long time){
        try {
            redisTemplate.opsForHash().putAll(key,map);
            if (time > 0){
                expire(key,time);
            }
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param item:
      *@return: * @return: boolean
      *@author: 你的辰宝
      *@time: 2021/11/4 2:11
     * 判断hash表中是否存在该项的值
    */
    public  boolean hHasKey(String key,String item){
        return redisTemplate.opsForHash().hasKey(key,item);
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param item:
     * @Param by:
      *@return: * @return: double
      *@author: 你的辰宝
      *@time: 2021/11/4 2:14
    hash递增，如果不存在，就会创建一个，并把新增后的值返回
     */
    public  double hincr(String key,String item,double by){
        return  redisTemplate.opsForHash().increment(key,item,by);
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param item:
     * @Param by:
      *@return: * @return: double
      *@author: 你的辰宝
      *@time: 2021/11/4 2:15
     hash递减
     */
    public  double hdecr(String key,String item,double by){
        return  redisTemplate.opsForHash().increment(key,item,-by);
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
      *@return: * @return: java.util.Set<java.lang.Object>
      *@author: 你的辰宝
      *@time: 2021/11/4 2:17
     根据key获取Set中所有值
     */
    public Set<Object> sGet(String key){
        try {
            return  redisTemplate.opsForSet().members(key);
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param value:
      *@return: * @return: boolean
      *@author: 你的辰宝
      *@time: 2021/11/4 2:19
     根据value从Set中查询是否存在
     */
    public boolean sHasKey(String key,Object value){
        try {
            return  redisTemplate.opsForSet().isMember(key,value);
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param values:
      *@return: * @return: long
      *@author: 你的辰宝
      *@time: 2021/11/4 2:21
    将数据放入Set中
     */
    public long sSet(String key,Object... values){
        try {
            return   redisTemplate.opsForSet().add(key,values);
        }catch (Exception e){
            e.printStackTrace();
            return  0;
        }
    }
    /**
     *@deprecated: RedisUtil
     *@parms: * @Param key:
     * @Param values:
     *@return: * @return: long
     *@author: 你的辰宝
     *@time: 2021/11/4 2:21
    将数据放入Set中
     */
    public long sSet(String key,long time,Object... values){
        try {
            Long count = redisTemplate.opsForSet().add(key,values);
            if (time > 0){
                expire(key,time);
            }
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return  0;
        }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
      *@return: * @return: long
      *@author: 你的辰宝
      *@time: 2021/11/4 2:24
     获取set缓存的长度
     */
    public long sGetSetSize(String key){
        try{
            return  redisTemplate.opsForSet().size(key);

        }catch (Exception e){
            e.printStackTrace();
            return  0;
        }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param value:
      *@return: * @return: long
      *@author: 你的辰宝
      *@time: 2021/11/4 2:27
      移除值为value
     */
    public  long setRemove(String key,Object... value){
        try {
            Long count=redisTemplate.opsForSet().remove(key, value);
            return  count;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }


    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param satrt:
     * @Param end:
      *@return: * @return: java.util.List<java.lang.Object>
      *@author: 你的辰宝
      *@time: 2021/11/4 2:34
     获取List缓存的内容
     */
    public List<Object> IGet(String key,long satrt,long end){
        try {
            return  redisTemplate.opsForList().range(key,satrt,end);
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
      *@return: * @return: long
      *@author: 你的辰宝
      *@time: 2021/11/4 2:42
        获取List缓存的长度
     */
    public long IGetListSize(String key){
        try {
            return  redisTemplate.opsForList().size(key);
        }catch (Exception e){
            e.printStackTrace();
            return  0;
        }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param index:
      *@return: * @return: java.lang.Object
      *@author: 你的辰宝
      *@time: 2021/11/4 2:43
     通过索引获取list的值
     */
    public Object IGetIndex(String key,long index){
        try {
            return  redisTemplate.opsForList().index(key,index);
        }catch (Exception e ){
            e.printStackTrace();
            return  null;
        }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param value:
      *@return: * @return: boolean
      *@author: 你的辰宝
      *@time: 2021/11/4 2:45
     将List放进缓存
     */
    public  boolean ISet(String key,Object value){
        try {
              redisTemplate.opsForList().rightPush(key,value);
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }

    /**
     *@deprecated: RedisUtil
     *@parms: * @Param key:
     * @Param value:
     *@return: * @return: boolean
     *@author: 你的辰宝
     *@time: 2021/11/4 2:45
    将List放进缓存
     */
    public  boolean ISet(String key,Object value,long time){
        try {
            redisTemplate.opsForList().rightPush(key,value);
            if (time > 0){
                expire(key,time);
            }
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param value:
      *@return: * @return: boolean
      *@author: 你的辰宝
      *@time: 2021/11/4 2:48
     将List放入缓存中
     */
    public  boolean ISet(String key,List<Object> value){
        try {
            redisTemplate.opsForList().rightPushAll(key,value);
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }

    /**
     *@deprecated: RedisUtil
     *@parms: * @Param key:
     * @Param value:
     *@return: * @return: boolean
     *@author: 你的辰宝
     *@time: 2021/11/4 2:48
    将List放入缓存中
     */
    public  boolean ISet(String key,List<Object> value,long time){
        try {
            redisTemplate.opsForList().rightPushAll(key,value);
            if (time > 0){
                expire(key,time);
            }
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param index:
     * @Param value:
      *@return: * @return: boolean
      *@author: 你的辰宝
      *@time: 2021/11/4 2:50
     根据索引修改List中的某条数据
     */
    public  boolean  IUpdateIndex(String key,long index,Object value){
        try {
            redisTemplate.opsForList().set(key, index, value);
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }
    }

    /**
      *@deprecated: RedisUtil
      *@parms: * @Param key:
     * @Param count:
     * @Param value:
      *@return: * @return: long
      *@author: 你的辰宝
      *@time: 2021/11/4 2:53
     移除N个值韦value
     */
    public long IRemove(String key,long count,Object value){
        try {
            long remove=redisTemplate.opsForList().remove(key, count, value);
            return remove;
        }catch (Exception e){
            e.printStackTrace();
            return  0;
        }
    }

}
